package com.fhz.day005

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.room.Room
import androidx.room.migration.Migration
import com.fhz.day005.test2.XiaoMingObserver
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        lifecycle.addObserver(XiaoMingObserver())

        val userDao = SqlData.getSqlData(this).getUserDao()

        val studentDao = SqlData.getSqlData(this).getStudentDao()

        insert.setOnClickListener{
            thread {

                val student = Student("daming")
                studentDao.insertStudent(student)

                val user = User("xiaoming", 16)
                val insertUser = userDao.insertUser(user)
                Log.i("insert", insertUser.toString())
            }
        }

        query.setOnClickListener {
            val queryAll = userDao.queryAll()
            Log.i("size",queryAll.size.toString())
        }


        update.setOnClickListener{
            val queryUserById = userDao.queryUserById(1)
            Log.i("query", queryUserById.toString())
            queryUserById.age = 20
            userDao.updateUser(queryUserById)
            Log.i("query", queryUserById.toString())
        }

        del.setOnClickListener {
            val user = User("xiaoming", 20)
            user.id = 1
            userDao.delUser(user)

            val queryAll = userDao.queryAll()
            Log.i("size",queryAll.size.toString())
        }


    }


}
